global path = "C:\Users\creto\Desktop\chinese_students_2017"


//OPEN LOG FILE
cap log close
*log using "$path/Logfiles/analysis_expweighted_08feb2021.log", replace


clear
set more off
set matsize 10000



/****************************************************************************/

****************       STEP 1: LOAD DATA     **************

/****************************************************************************/


****** NEW DATA FILE FOR REPLICATION CREATED BY KEVIN *******

use "$path/Submissions/ReStat/Replication Files 2022/kswxy_data.dta", clear
	
			*---------------------------------------------------------------------------*			
/****************************************************************************/

*************   SUMMARY STATS TABLE ***************

******************************************************************************

*----------------------------------------------------------------------------*
	drop if citycode==.
	
	gen sample = 1 if balanced==1 & popwt03!=. 
	egen minsample = min(sample), by(citycode)
	drop sample
	rename minsample sample

	gen exports = dvalue/10000
	
	gen gdppc_true = gdppc*10
	
	gen stud_ot = stud_1 + stud_2 + stud_7 + stud_8
	gen stud_nonfunded = stud_tot - stud_univ_funded
	gen gdp_true = gdp*10000
	gen ln_gdp_true=ln(gdp_true)
	
	foreach x in as ba ma dr ot stem ss arts bus hi_admrate_p0_25 hi_admrate_p25_50 hi_admrate_p50_75 hi_admrate_p75_100 univ_funded nonfunded {
	gen sh_`x' = stud_`x'/stud_tot
	}
	
	
	//CALCULATE SUMMARY STATS HERE (ADD NEW VARIABLES TO THE FOREACH LOOP)
	foreach var of varlist stud_tot sh_as sh_ba sh_ma sh_dr sh_ot sh_stem sh_ss sh_arts ///
						   sh_hi_admrate_p0_25 sh_hi_admrate_p25_50 sh_hi_admrate_p50_75 sh_hi_admrate_p75_100 ///
						   sh_univ_funded sh_nonfunded ln_dvalue pop ln_gdp_true gdp exports gdppc_true {
						   
			eststo `var'_0: estpost tabstat `var' if year==2000 & sample==1, statistics(mean sd N) columns(statistics) listwise
			distinct citycode if year==2004 & sample==1
			estadd scalar cities=r(ndistinct)

			eststo `var'_1: estpost tabstat `var' if year==2013 & sample==1, statistics(mean sd N) columns(statistics) listwise
			distinct citycode if year==2013 & sample==1
			estadd scalar cities=r(ndistinct)
		}	
	
	
				
	*---------------------------------------------------------------------------*			
/****************************************************************************/

*************   REGRESSION ANALYSIS   ***************

******************************************************************************
*----------------------------------------------------------------------------*

//SET CONTROLS:
global controls_empwtd "contract_cons_empwtd90_isic inputtariff_empwtd90_isic chinatariff_empwtd90_isic share_revenue_empwtd90_isic"
label var ntrgap_empwtd90_isic "\$PNTR^{1990,EMP}_{c}\$" 


**DROP CITY MISSING OCDE
drop if citycode==.



sum ntrgap_empwtd90_isic if year==2013 & balanced==1, de
				local iqr_ivntremp90 = r(p75) - r(p25)
				eststo clear
				
				
eststo A: reg shc_stud_tot_0213 ntrgap_empwtd90_isic if year==2013 & balanced==1 , cluster(citycode)
estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

eststo B: reg shc_stud_tot_0213 ntrgap_empwtd90_isic contract_cons_empwtd90_isic if year==2013 & balanced==1 , cluster(citycode)
estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

eststo C: reg shc_stud_tot_0213 ntrgap_empwtd90_isic contract_cons_empwtd90_isic inputtariff_empwtd90_isic if year==2013 & balanced==1 , cluster(citycode)
estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

eststo D: reg shc_stud_tot_0213 ntrgap_empwtd90_isic contract_cons_empwtd90_isic inputtariff_empwtd90_isic chinatariff_empwtd90_isic if year==2013 & balanced==1 , cluster(citycode)
estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)

eststo E: reg shc_stud_tot_0213 ntrgap_empwtd90_isic $controls_empwtd if year==2013 & balanced==1 , cluster(citycode)
estadd local iqreffect = round(`iqr_ivntremp90'*_b[ntrgap_empwtd90_isic]*1000,1)


				esttab A B C D E , b(3) se(3) /// 
						keep(ntrgap_empwtd90_isic $controls) star(* 0.10 ** 0.05 *** 0.01) stats(iqreffect ymean N r2, label("\textit{Interquartile Effect:}\\ \textit{$\Delta$ Students per 1m Pop.}" "Mean Dep Var." "Obs." "R2") fmt(%12.0fc %12.3fc %12.0fc %12.3fc)) ///
						mtitles("No Controls" "\shortstack{+Control for\\Contract Intensity}" "\shortstack{+Control for\\Import Tariffs}" "\shortstack{+Control for\\Input Tariffs}" "\shortstack{+Control for\\Export Licenses}") ///
						mgroups("2002-2013", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
						substitute(\_ _) label booktabs nonotes
